//
//  SEGExpressRequestModel.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 16/8/19.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGExpressRequestModel.h"

//收件id
#define kReceiveExpressId [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rest-api/v1/frontendExpress/receiveIds.json",nil]
//收件列表
#define kReceiveExpressList [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rest-api/v1/frontendExpress/receiveList.json",nil]
//收件详情
#define kReceiveDetail [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rest-api/v1/frontendExpress/receiveDetail.json",nil]
//发件详情
#define kSendDetail [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rest-api/v1/frontendExpress/sendDetail.json",nil]

@implementation SEGExpressRequestModel
/**
 *  单例统一管理Access涉及的网络请求
 *
 *  @return 实例
 */
+ (instancetype)sharedRequestModel
{
    static SEGExpressRequestModel* instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    
    return instance;
}

/**
 *  请求收件id 可替换[[BusinessManager sharedManager].expressEntityManager requestReceiveExpressId];;
 *
 *  @param finished 网络请求完成后的操作
 */
- (void)requestReceiveExpressId:(SEGNetworkReponse)finished {
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:kReceiveExpressId parameters:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,nil,codeType,errorMessage);
            return ;
        }
        
        NSDictionary *dict = result;

        // 数据返回正常
        if ([[dict objectForKey:@"code"] isEqualToString:@"0"]) {
            //  拼参数ids
            NSMutableString *ids = [NSMutableString stringWithString:@""];
            NSArray *data = [dict objectForKey:@"data"];
            for (int i = 0; i < data.count; i++) {
                NSString *idString = [[data[i] objectForKey:@"id"] stringValue];
                if (i == 0) {
                    [ids appendString:idString];
                }else {
                    [ids appendFormat:@",%@",idString];
                }
            }
            
            // 请求列表
            NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:ids,@"ids", nil];
            [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:kReceiveExpressList parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                finished(isSuccessed, result, codeType, errorMessage);
            }];
        }else {
            finished(NO,result,codeType,errorMessage);
        }
    }];
}


/**
 *  请求收件详情 可替换[[BusinessManager sharedManager].expressEntityManager requestDetailExpressEntityWithID:
 *
 *  @param finished 网络请求完成后的操作
 */
- (void)requestReceiveExpressDetailWithExpressSid:(NSString *)expressSid finished:(SEGNetworkReponse)finished {
    
    // 3. 网路请求方法(必须添加)
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:expressSid,@"expressSid", nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:kReceiveDetail parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        finished(isSuccessed, result, codeType, errorMessage);
    }];
}


-(void)requestDetailExpressEntityWithID:(NSString *)aID withType:(NSString*)type finished:(SEGNetworkReponse)finished{
    
    // 请求地址
    NSString *url = @"";
    
    if ([type isEqualToString:@"0"]) {
        url = kReceiveDetail;
    }else {
        url = kSendDetail;
    }
    
    // 网路请求方法
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:[SEGUtils ValueToString:aID],@"expressSid", nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:url parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        finished(isSuccessed, result, codeType, errorMessage);
    }];
}
@end
